import {Page} from '@/base/Page';
import {deleteAffiliate, getAffiliateList} from '@/base/api/AffiliateApi';
import {getAsleep} from '@/base/api/AsleepApi'
import {AsleepVo} from '@/base/model/AsleepVo';
import {isEmpty} from '@/base/util/Strings';
import {getRouseCount} from '@/base/api/RouseCountApi';
import {RouseCount} from '@/base/model/RouseCount';
import {getAsleepAnalyzeApi} from '@/base/api/AsleepAnalyzeApi';
import {AsleepAnalyze} from '@/base/model/AsleepAnalyze';
import {getSleepWarningApi} from '@/base/api/SleepWarningApi';
import {SleepWarning} from '@/base/model/SleepWarning';
import {getPlayRankApi} from '@/base/api/PlayRankApi';
import {PlayRank} from '@/base/model/PlayRank';
import {formatDate} from '@/base/util/Dates';

class Form {
  nickname = '';
  createBegin = '';
  createEnd = '';
  date = [];
}

class Time {
  createBegin = formatDate(new Date(), 'yyyy/MM/dd hh:mm:ss');
  createEnd = formatDate(new Date(), 'yyyy/MM/dd hh:mm:ss');
}

export class DetailBloc {
  form = new Form();
  time = new Time()
  rouseCount = new RouseCount()

  paged: Page<AsleepVo> = new Page<AsleepVo>([], 0, 1, 10, 0, []);
  AsleepAnalyze: Page<AsleepAnalyze> = new Page<AsleepAnalyze>([], 0, 1, 10, 0, []);
  SleepWarning: Page<SleepWarning> = new Page<SleepWarning>([], 0, 1, 10, 0, []);
  loading = false;
  memberId = 0;
  isShowDetailModal = false;
  detailMemberId = 0;
  playRank: Array<PlayRank> = []

  load(pageIndex: number, pageSize: number) {
    this.loading = true;
    return getAsleep({
      pageIndex,
      pageSize,
      sortDirection: 'ASC',
      memberId: this.memberId,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }

  getRouseCount() {
    return getRouseCount({
      memberId: this.memberId,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.rouseCount = res.data
    }).finally(() => {
      this.loading = false;
    });
  }

  getAsleepAnalyze(pageIndex: number, pageSize: number) {
    return getAsleepAnalyzeApi({
      pageIndex,
      pageSize,
      sortDirection: 'ASC',
      memberId: this.memberId,
      createBegin: this.time.createBegin,
      createEnd: this.time.createEnd
    }).then((res) => {
      this.AsleepAnalyze = res.data
    }).finally(() => {
      this.loading = false;
    });
  }

  getSleepWarning(pageIndex: number, pageSize: number) {
    return getSleepWarningApi({
      pageIndex,
      pageSize,
      sortDirection: 'ASC',
      memberId: this.memberId,
      createBegin: this.time.createBegin,
      createEnd: this.time.createEnd
    }).then((res) => {
      this.SleepWarning = res.data
    }).finally(() => {
      this.loading = false;
    });
  }

  getPlayRank() {
    return getPlayRankApi({
      memberId: this.memberId,
      createBegin: this.time.createBegin,
      createEnd: this.time.createEnd,
      num: 5
    }).then((res) => {
      this.playRank = res.data
    }).finally(() => {
      this.loading = false;
    });
  }
}
